﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Tencent.OA.ACM.Proxy
{
    /// <summary>
    /// WCF代理管理类
    /// </summary>
    public partial class ProxyManager
    {
        private const string ContextItemKey = "ClientProxyContainer";
        private static Dictionary<Type, object> _containter = null;
        /// <summary>
        /// 类型与对象字典
        /// </summary>
        private static Dictionary<Type, object> Containter
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    if (_containter == null)
                    {
                        _containter = new Dictionary<Type, object>();
                    }
                    return _containter;
                }
                else
                {
                    if (HttpContext.Current.Items[ContextItemKey] == null)
                    {
                        HttpContext.Current.Items[ContextItemKey] = new Dictionary<Type, object>();
                    }
                    return (Dictionary<Type, object>)HttpContext.Current.Items[ContextItemKey];
                }
            }
        }
        /// <summary>
        /// 获取一个具体的代理对象，并打开它
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="endpointAddress">WCF服务地址</param>
        /// <param name="binding">绑定类型</param>
        /// <returns></returns>
        public static T GetProxy<T>(EndpointAddress endpointAddress, Binding binding) where T : class
        {
            object t;
            if (!Containter.TryGetValue(typeof(T), out t))
            {
                ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpointAddress);
                t = channelFactory.CreateChannel();
                ((IChannel)t).Open();
                Containter.Add(typeof(T), t);
            }
            else
            {
                if (((IChannel)t).State != CommunicationState.Opened)
                {
                    ((IChannel)t).Open();
                }
            }

            return (T)t;
        }
        /// <summary>
        /// 获取一个代理对象。并打开使用
        /// </summary>
        /// <typeparam name="T">类型名称</typeparam>
        /// <returns></returns>
        public static T GetProxy<T>() where T : class
        {
            object t;
            if (!Containter.TryGetValue(typeof(T), out t))
            {
                ChannelFactory<T> channelFactory = new ChannelFactory<T>(typeof(T).Name);
                t = channelFactory.CreateChannel();
                ((IChannel)t).Open();
                Containter.Add(typeof(T), t);
            }
            else
            {
                if (((IChannel)t).State != CommunicationState.Opened)
                {
                    ((IChannel)t).Open();
                }
            }

            return (T)t;
        }
        /// <summary>
        /// 关闭代理对象
        /// </summary>
        /// <param name="client"></param>
        public static void DistoryProxy(object client)
        {
            if (client is IChannel)
            {
                IChannel channel = ((IChannel)client);
                if (channel.State != CommunicationState.Closed)
                {
                    channel.Close();
                }
            }
        }
        /// <summary>
        /// 关闭所有代理对象
        /// </summary>
        public static void DistoryAllProxy()
        {
            if (Containter != null)
            {
                foreach (KeyValuePair<Type, object> client in Containter)
                {
                    DistoryProxy(client.Value);
                }
                Containter.Clear();
            }
        }
    }
}
